#pragma once
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

#include "Building.h"
#include "Room.h"

using namespace std;

class BuildingDatabase
{
private:
	vector<Building> v;
public:

	BuildingDatabase(){ }

	~BuildingDatabase(){ }

	void ReadFile(string filename)
	{
		v.clear();
		string str, row;
		ifstream fin;
		try
		{
			fin.open(filename);
			while (getline(fin, row))
			{
				istringstream iss(row);
				getline(iss, str, '=');
				if (str.compare("Building") == 0)
				{
					Building b;
					iss >> b;
					if (b.getInfo().compare("N/A") != 0)
					{
						v.push_back(b);
					}
				}
				if (str.compare("Room") == 0)
				{
					Room r;
					iss >> r;
					if (r.getArea() != -1)
					{
						v[v.size() - 1].Add(r);
					}
				}
			}
		}
		catch (exception e)
		{
			cout << "Exception thrown for file: " << filename << endl;
			cout << e.what();
		}
		fin.close();
	}

	void WriteFile(string filename)
	{
		ofstream fout;
		try
		{
			fout.open(filename);
			for (auto &b : v)
			{
				fout << b;
			}
		}
		catch (exception e)
		{
			cout << "Exception thrown for file: " << filename << endl;
			cout << e.what();
		}
		fout.close();
	}

	void Add(Building b)
	{
		v.push_back(b);
	}

	string List()
	{
		ostringstream oss;
		for (auto &b : v)
		{
			oss << b;
		}
		return oss.str();
	}

	double Area()
	{
		double total = 0;
		for (auto &b : v)
		{
			total += b.Area();
		}
		return total;
	}

	void Delete(int i)
	{
		v.erase(v.begin() + i);
	}

	string SearchBuilding(string searchterm)
	{
		ostringstream oss;
		for (auto &b : v)
		{
			if (b.getInfo().compare(searchterm) == 0)
			{
				oss << b;
				return oss.str();
			}
		}
	}

	void ListBuildings()
	{
		for (int i = 0; i < v.size(); i++)
		{
			cout << i << ": " << v[i].getInfo() << endl;
		}
	}

	void Menu()
	{
		bool runMenu = true;
		while (runMenu)
		{
			system("cls");
			cout << "1: Read from file" << endl;
			cout << "2: Write to file" << endl;
			cout << "3: Calculate Area" << endl;
			cout << "4: Search for building" << endl;
			cout << "5: Add new building" << endl;
			cout << "6: Delete building" << endl;
			cout << endl;
			cout << "0: Exit" << endl;

			int input;
			cin >> input;
			cin.get(); //remove '\n'

			//helper variables
			string str;
			Building b;
			int i;

			system("cls");
			switch (input)
			{
			case 1: //Read from file
				cout << "Read from what file: ";
				getline(cin, str);
				v.clear();
				ReadFile(str);
				break;
			case 2: //Write to file
				cout << "Write to what file: ";
				getline(cin, str);
				WriteFile(str);
				break;
			case 3: //Calculate area
				cout << "Total area for all buildings: " << Area() << endl;
				system("pause");
				break;
			case 4: //Search for building
				cout << "Search term: ";
				getline(cin, str);
				cout << SearchBuilding(str);
				system("pause");
				break;
			case 5: //Add new building
				b.Read();
				v.push_back(b);
				break;
			case 6: // Delete building
				ListBuildings();
				cout << "Delete building with index: ";
				cin >> i;
				cin.get();
				Delete(i);
				break;
			default: runMenu = false; break;
			}
		}
	}
};

